<template>
	<view class="main">
		<view class="db_title f30 bot_line">
			待办事项详情
		</view>
		<view class="form">
			<view class="db_row bot_line">
				<view class="icon">
					<image src="../../static/image/sxmc.png" mode="widthFix"></image>
				</view>
				<view class="con gui-flex gui-space-between">
					<view class="gui-form-body gui-flex gui-align-items-center">
						<input type="text" class="gui-form-input f24" v-model="formData.title" name="title"
							placeholder="请输入事项名称" />
					</view>
					<text class="gui-form-icon gui-icons gui-text-center gui-color-gray"
						v-if="formData.title.length >= 1" @click="clearName2">&#xe78a;</text>
					<view class="label f24">
						事项名称
					</view>
				</view>
			</view>
			<view class="db_row bot_line">
				<view class="icon">
					<image src="../../static/image/sxrq.png" mode="widthFix"></image>
				</view>
				<view class="con gui-flex gui-space-between">
					<view class="input_warp">
						<view class="val f24">
							{{formData.date}}
						</view>
					</view>
					<view class="label f24">
						事项时间
					</view>
				</view>
			</view>
			<view class="db_row bot_line">
				<view class="icon">
					<image src="../../static/image/xgkh.png" mode="widthFix"></image>
				</view>
				<view class="con gui-flex gui-space-between">
					<view class="input_warp f24" style="padding-left: 26rpx;">
						<text v-for="(item, index) in clientList" :key="index">{{item.name}}</text>
					</view>
					<view class="label f24">
						客户相关（选填）
					</view>
				</view>
			</view>
			<!-- <view class="db_row bot_line" v-if="clientList.length > 0">
				<view class="icon"></view>
				<view class="con gui-flex gui-wrap">
					<view class="selectWarp f24" @click="delClient(index)" v-for="(item,index) in clientList"
						:key="index">
						{{item.name}}
						<text class="gui-icons" style="margin-left: 10rpx;">&#xe7a5;</text>
					</view>
				</view>
			</view> -->

			<view class="db_row bot_line" style="align-items: flex-start;">
				<view class="icon">
					<image src="../../static/image/sx.png" mode="widthFix"></image>
				</view>
				<view class="con">
					<view class="sx_item mt_20" v-for="(item,index) in formData.itemList" :key="index">
						<view class="f24" style="padding-left: 24rpx;">
							{{item.value}}
						</view>
						<text class="gui-icons f24" @click="delItem(index)" v-if="index > 0">&#xe7a5;</text>
					</view>
				</view>
			</view>
			<view class="db_row bot_line" style="align-items: flex-start;">
				<view class="icon">
					<image src="../../static/image/bz.png" mode="widthFix"></image>
				</view>
				<view class="con" style="line-height: 30rpx;">
					<view class="textarea_warp f24" style="min-height: 200rpx;" placeholder="备注(选填)">
						{{formData.remark}}
					</view>
				</view>
			</view>
			<view class="db_row bot_line" v-if="formData.state == 0">
				<view class="icon gui-flex gui-align-items-center" style="width: 200rpx;">
					<image src="../../static/image/sxzt.png" mode="widthFix"></image>
					<text class="f24" style="padding-left: 24rpx;">事项状态：</text>
				</view>
				<view class="con gui-flex gui-justify-content-end">
					<view class="status_warp f24" @click="updateBacklog(2)">
						已取消
					</view>
					<view class="status_warp f24" style="margin-left: 20rpx;" @click="updateBacklog(1)">
						已完成
					</view>
				</view>
			</view>
			<view class="db_row bot_line" v-else>
				<view class="icon gui-flex gui-align-items-center" style="width: 200rpx;">
					<image src="../../static/image/sxzt.png" mode="widthFix"></image>
					<text class="f24" style="padding-left: 24rpx;">事项状态：</text>
				</view>
				<view class="con gui-flex gui-justify-content-end">
					<view class="f24" v-if="formData.state == 2">
						已取消
					</view>
					<view class="f24" v-if="formData.state == 1" style="margin-left: 20rpx;color: #3980fe;">
						已完成
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var timer;
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				backlogId: '',
				today: '',
				isClick: true,
				formData: {
					title: '',
					itemList: [{
						value: ''
					}],
					date: '',
					remark: ''
				},
				date: '',
				clientList: []
			}
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		onLoad(e) {
			if(e.time) {
				this.today = e.time;
			}
			if(e.id) {
				this.backlogId = e.id;
				this.loadData();
			}
		},
		onUnload() {
			clearTimeout(timer);
			uni.$off('selectChange')
		},
		methods: {
			// 修改待办事项
			updateBacklog(state) {
				let _this = this;
				uni.showModal({
					title: '提示',
					content: '确定要修改事项状态吗？',
					success: function(res) {
						if (res.confirm) {
							_this.$api.updateBacklog({
								id: _this.backlogId,
								state
							}).then(res => {
								_this.$u.msg("修改成功")
								uni.$emit('updateBacklog')
								timer = setTimeout(() => {
									_this.$u.back();
								}, 1000)
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				
			},
			loadData() {
				this.$api.backlogDetail({
					backlogId: this.backlogId
				}).then(res => {
					let {
						title,
						date,
						clientId,
						clientName,
						event,
						remark,
						state
					} = res.data;
					this.formData.title = title;
					this.formData.date = date;
					clientId = clientId + '';
					if(clientId && clientName) {
						let clientIdList = clientId.split(',');
						let nameList = clientName.split(',');
						clientIdList.forEach((e, index) => {
							this.clientList.push({
								id: e,
								name: nameList[index]
							})
						})
					}
					this.formData.itemList = JSON.parse(event);
					this.formData.remark = (remark) ? remark : '无备注';
					this.formData.state = state;
				})
			},
			addSx() {
				this.formData.itemList.push({
					value: ''
				})
			},
			// 删除客户
			delClient(index) {
				this.clientList.splice(index, 1);
			},
			delItem(index) {
				this.formData.itemList.splice(index, 1)
			},
			bindDateChange: function(e) {
				this.date = e.detail.value
			},
			bindTimeChange: function(e) {
				this.formData.date = e.detail.value
			},
			// 清空输入框的值
			clearName2: function() {
				console.log(11);
				this.formData.title = '';
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			// 新增待办
			addBacklog() {
				let formParam = {
					title: '',
					date: '',
					event: '',
					clientId: '',
					clientName: '',
					remark: ''
				}
				formParam.title = this.formData.title;
				formParam.date = this.today + ' ' + this.formData.date;
				if (this.formData.itemList.length > 0) {
					// let event = this.formData.itemList.map(item => item.value);
					formParam.event = JSON.stringify(this.formData.itemList);
				}
				if (this.clientList.length > 0) {
					let idList = [];
					let nameList = [];
					this.clientList.forEach(e => {
						idList.push(e.id);
						nameList.push(e.name);
					})
					formParam.clientId = idList.join(',');
					formParam.clientName = nameList.join(',');
				}
				formParam.remark = this.formData.remark;
				if(this.isClick) {
					this.isClick = !this.isClick;
				}else {
					return;
				}
				this.$api.addBacklog(formParam).then(res => {
					this.$u.msg("添加成功");
					setTimeout(() => {
						this.$u.back();
					}, 1000)
				}).finally(e => {
					this.isClick = true;
				})
			}
		}
	}
</script>

<style lang="scss">
	.main {
		padding: 36rpx;
	}

	.btn_warp {
		margin-top: 80rpx;
	}

	.btn_log {
		width: 600rpx;
		height: 86rpx;
		background: linear-gradient(180deg, #7EC1FF 0%, #0A60FF 100%);
		border-radius: 50rpx 50rpx 50rpx 50rpx;
		opacity: 1;
		font-size: 28rpx;
		color: rgba(255, 255, 255, 0.85);
		line-height: 86rpx;
	}

	.selectWarp {
		padding: 0 30rpx;
		width: auto;
		height: 60rpx;
		background: rgba(14, 110, 255, 0.15);
		border-radius: 30rpx;
		border: 1rpx solid rgba(165, 128, 232, 0.2);
		display: flex;
		align-items: center;
		justify-content: center;
		color: #0E6EFF;
		margin-right: 20rpx;
		margin-bottom: 10rpx;
	}
	
	.status_warp {
		width: 135rpx;
		height: 45rpx;
		background: linear-gradient(180deg, rgba(24, 144, 255, 0.15) 0%, rgba(10, 96, 255, 0.15) 100%);
		border-radius: 50rpx;
		border: 1rpx solid #0E6EFF;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #0E6EFF;
	}
</style>